<?php

class HTech_Model_Inspector extends Zend_Db_Table_Abstract
{
    public function getItems()
    {       
        $locations = new HTech_Model_Locations();
        $officers = new HTech_Model_Officers();
        $lines = new HTech_Model_ProductLines();
        $programs = new HTech_Model_Programs();
        $products = new HTech_Model_LoanProducts();
        
        $tree = array(
            'Branch' => array(
                'sType'    => 'Location',
                'nodes'    => $locations->fetchAll($locations->select()->where('Category = "Branch"'))->toArray(),
                'key'      => 'LocationId',
                'name'     => 'Name',
                'children' => array('Officer' => 'LocationId','Agency' => 'ParentLocationId'),
                'url'      => '/admin/location/view/id/%s'
             ),
            'Agency' => array(
                'sType'    => 'Location',
                'nodes'    => $locations->fetchAll($locations->select()->where('Category = "Agency"'))->toArray(),
                'key'      => 'LocationId',
                'name'     => 'Name',
                'children' => array('Officer' => 'LocationId'),
                'url'      => '/admin/location/view/id/%s'
             ),
            'Officer' => array(
                'nodes'    => $officers->fetchAll()->toArray(),
                'key'      => 'OfficerId',
                'name'     => 'FullName',
                'name2'    => 'Username',
                'url'      => '/admin/officer/view/id/%s'
             ),
            'Line' => array(
                'nodes'    => $lines->fetchAll()->toArray(),
                'key'      => 'ProductLineId',
                'name'     => 'ProductLine',
                'children' => array('Program' => 'ProductLineId'),
                'url'      => '/admin/product-line/view/id/%s'
             ),
            'Program' => array(
                'nodes'    => $programs->fetchAll()->toArray(),
                'key'      => 'ProgramId',
                'name'     => 'Program',
                'children' => array('Product' => 'ProgramId'),
                'url'      => '/admin/program/view/id/%s'
             ),
            'Product' => array(
                'nodes'    => $products->fetchAll()->toArray(),
                'key'      => 'LoanProductId',
                'name'     => 'Name',
                'url'      => '/loan/loan-product/view/id/%s'
             )
        );
        
        $pf = Zend_Registry::get('db')->query("select Subject, coalesce(OfficerUserId,LocationId,ProductId,ProgramId,LineId) Id, Score, Color 
            from performance_data where Date = curdate() and Metric = 'Score'")->fetchAll();
        $pd = array();
        foreach($pf as $row) {
            $pd[$row->Subject][$row->Id]['Color'] = $row->Color;
        }
        
        $items = array();
        $c = 0;
        foreach($tree as $type => $data) {
            foreach($data['nodes'] as $node) { 
                $sType = isset($data['sType']) ? $data['sType'] : $type;
                $items[$c]['json_id'] = $type . '.' . $node[$data['key']];
                $items[$c]['type'] = $type;
                $items[$c]['id'] = $node[$data['key']]; 
                $items[$c]['url'] = sprintf($data['url'],$node[$data['key']]);
                $items[$c]['name'] = $node[$data['name']]; 
                $items[$c]['color'] = isset($pd[$sType][$node[$data['key']]]['Color']) ? $pd[$sType][$node[$data['key']]]['Color'] : '';
                if (isset($data['children'])&&is_array($data['children'])) {
                    $children = array(0 => array());
                    $i = 1;
                    foreach($data['children'] as $ch => $pk) {
                        $children[$i] = array_merge($children[$i-1],self::_getNodeChildren($tree,$data,$node,$ch,$pk));
                        if (count($children[$i])) $items[$c]['children'] = $children[$i];
                        $i++;
                    }
                }
                $c++;
            }
        }
        return $items;
    }
    
    protected function _getNodeChildren($tree,$data,$node,$ch,$pk)
    {
        foreach($tree[$ch]['nodes'] as $children) {
            if (isset($children[$pk])&&($children[$pk] == $node[$data['key']])) {
                $elm[] = array('_reference' => $ch . '.' . $children[$tree[$ch]['key']]);
            }
        }
        return isset($elm) ? $elm : array();
    }
    
}
